package com.zide.ams.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zide.ams.domain.po.AmsArticle;
import com.zide.ams.domain.po.AmsComment;
import com.zide.ams.domain.vo.result.AmsCommentResultVO;
import com.zide.ams.domain.vo.save.AmsCommentSaveVO;
import com.zide.ams.domain.vo.search.AmsCommentSearchVO;
import com.zide.ams.mapper.AmsArticleMapper;
import com.zide.ams.mapper.AmsCommentMapper;
import com.zide.ams.service.IAmsCommentService;
import com.zide.common.constant.HttpStatus;
import com.zide.common.core.domain.PageResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.zide.common.core.vo.CommonParamIdVO;
import com.zide.common.exception.ServiceException;
import com.zide.common.utils.SecurityUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文章评论Service业务层处理
 * 
 * @author zide
 * @date 2024-04-29
 */
@Slf4j
@Service
public class AmsCommentServiceImpl implements IAmsCommentService {
    @Resource
    private AmsCommentMapper amsCommentMapper;
    @Resource
    private AmsArticleMapper amsArticleMapper;

    /**
     * 查询文章评论列表
     */
    @Override
    public PageResult<AmsCommentResultVO> searchAmsCommentPageList(AmsCommentSearchVO searchVO) {
        PageResult<AmsCommentResultVO> result = amsCommentMapper.selectPage(searchVO);
        List<AmsCommentResultVO> commentList = result.getList();
        List<Long> parentIds = commentList.stream().map(AmsCommentResultVO::getId).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(parentIds)) {
            // 查询评论回复
            List<AmsCommentResultVO> childrenList = amsCommentMapper.selectChildrenList(parentIds);
            for (AmsCommentResultVO amsCommentResultVO : commentList) {
                Long parentId = amsCommentResultVO.getId();
                List<AmsCommentResultVO> children = childrenList.stream().filter(item -> item.getParentId() == parentId).collect(Collectors.toList());
                amsCommentResultVO.setChildren(children);
            }
        }
        return result;
    }

    /**
     * 查询文章评论详情
     */
    @Override
    public AmsCommentResultVO selectAmsCommentById(Long id) {
            AmsComment dbInfo = amsCommentMapper.selectById(id);
            AmsCommentResultVO result = BeanUtil.copyProperties(dbInfo,AmsCommentResultVO.class);
        return result;
    }

    /**
     * 新增文章评论
     */
    @Transactional
    @Override
    public int saveAmsComment(AmsCommentSaveVO saveVO) {
        // 获取当前用户id
        long loginIdAsLong = SecurityUtils.getMemberUserId();
        if(loginIdAsLong == 0) {
            throw new ServiceException(HttpStatus.FORBIDDEN,"");
        }
        AmsComment insertBean = BeanUtil.copyProperties(saveVO,AmsComment.class);
        insertBean.setCreateTime(LocalDateTime.now());
        Long articleId = saveVO.getArticleId();
        LambdaUpdateWrapper<AmsArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("comment_volume = comment_volume + 1").eq(AmsArticle::getId,articleId);
        amsArticleMapper.update(wrapper);
        insertBean.setUserId(loginIdAsLong);
        return amsCommentMapper.insert(insertBean);
    }

    /**
     * 修改文章评论
     */
    @Override
    public int updateAmsComment(AmsCommentSaveVO saveVO) {
        AmsComment updateBean = BeanUtil.copyProperties(saveVO,AmsComment.class);
        return amsCommentMapper.updateById(updateBean);
    }

    /**
     * 批量删除文章评论
     */
    @Override
    public int removeAmsCommentByIds(CommonParamIdVO idVO) {
        if(ArrayUtil.isNotEmpty(idVO.getIds())) {
            return amsCommentMapper.deleteBatchIds(Arrays.asList(idVO.getIds()));
        }else {
            return amsCommentMapper.deleteById(idVO.getId());
        }

    }
}
